// pages/yuyue/index.js
const util = require('../../utils/util.js');
const app = getApp();
var sendPost = app.globalData.sendPost;
var globalData = getApp().globalData;
var date = util.formatDate(new Date());
Page({
    /**
     * 页面的初始数据
     */
    data: {
        date: '',
        gw_index: 0, //默认接待顾问
        sj_index: 0,
        sj_time: ['上午', '下午'],
        sellerArr: [],
        name:'',
        seller_id: '',
        time: '上午',
        num: '',
        tel: '',
        seller: '',
        seller_sel: false,
        seller_head: '', //置业顾问头像
        seller_name: '', //置业顾问姓名
        qian_img: globalData.qian_img,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      let _this = this;
      _this.setData({
          tel: options.tel,
          seller_id: options.id,
          seller_sel: options.seller_sel,
          seller_name: options.name,
          seller_head: options.head_img,
          seller_tel: options.tel,
      })
      _this.getData();
      _this.setData({
            date: date,
        })

    },
    //日期选择
    bindDateChange: function(e) {
        this.setData({
            date: e.detail.value
        })
    },
    //上午/下午
    bindTimeChange: function(e) {
        this.setData({
            sj_index: e.detail.value,
            time: this.data.sj_time[e.detail.value]
        })
    },
    //置业顾问详情页（名片）
    toSellerinfo:function(){
      var id = this.data.seller_id
      wx.navigateTo({
        url: '../seller_info/index?id=' + id,
      })
    },
    //电话咨询
    telClick: function(e) {
        let tel = e.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: tel,
        })
    },
    //接待顾问
    changeSeller(e) {
        this.setData({
            gw_index: e.detail.value,
            seller_id: this.data.sellerArr[e.detail.value].id,
            seller_sel: true,
            seller_head: this.data.sellerArr[e.detail.value].head_img,
            seller_name: this.data.sellerArr[e.detail.value].name,
            seller_tel: this.data.sellerArr[e.detail.value].mobile,
        });
    },
    // input输入事件
    inputChange(event) {
        let key = event.currentTarget.dataset.key;
        let value = event.detail.value;
        this.setData({
            [key]: value
        })
    },
    //确认预约
    yuyueTap: function() {
        var _this = this;
        _this.submitStep();
        if (_this.submitStep()) {
            _this.submitData();
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    // 进入页面获取顾问
    getData() {
        let data = {
            pid: globalData.pid,
        }
        globalData.sendPost('project/adviser-list', data).then(res => {   
            if (res.status == 1) {
              this.setData({
                sellerArr: res.data
              })
            }
        })
    },
    submitData() {
        let data = {
            aid: this.data.seller_id,
            openId: globalData.openId,
            name: this.data.name,
            mobile: this.data.tel,
            day: this.data.date,
            time: this.data.time,
            num: this.data.num,
        }
        globalData.sendPost('adviser/appoint', data).then(res => {
            if (res.status == 1) {
                wx.showToast({
                    title: '订阅成功',
                    icon: 'success',
                    duration: 2000,
                    success() {
                        wx.navigateBack();
                    }
                })
            }
        })
    },
    submitStep() {
        if (!this.data.name) {
          getApp().globalData.showHint('请填写您的姓名');
          return false;
        }
        if (!this.data.tel || (!/^1[3|5|6|7|8|9]\d{9}$/.test(this.data.tel))) {
            getApp().globalData.showHint('电话错误');
            return false;
        }
        if (!this.data.num) {
            getApp().globalData.showHint('请填写到访人数');
            return false;
        }
        if (!this.data.seller_id) {
          getApp().globalData.showHint('请选择置业顾问');
          return false;
        }
        return true;
    },
})